package com.jinyuanchun.self.covid19;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReadFromFile {
    public static void readByLines(String fileName) {
        // 构造文件对象
        File file = new File(fileName);
        BufferedReader reader = null;
        try{
            // 构造一个 打开文件的类  FileReader 相当于 perl 中的 open F,"file";
            // 再开启文件到内存的一个通信管道 BufferedReader  相当于 perl 的F （文件句柄）
            reader = new BufferedReader(new FileReader(file));
            String tmpString = null;
            int line = 1;
            while ((tmpString = reader.readLine()) != null) {
                //
                System.out.println("line : "+line+" : "+tmpString);
                line++;
            }
            reader.close();
        } catch (IOException e){
            e.printStackTrace();
        } finally {
            if (reader != null){
                try{
                    reader.close();
                } catch (IOException e1) {

                }
            }
        }
    }
    public static Map<String,Map<String,String>> read2Hash(String fileName,String key){
        File file = new File(fileName);
        BufferedReader reader = null;

        try{
            reader = new BufferedReader(new FileReader(file));
            // 定义初始变量
            Map<String,Map<String,String>> result = new HashMap<>();
            String tmpString = null;
            int keyIndex = 0;
            String keyValue = null;
            List<String> headers = new ArrayList<>();
            while ((tmpString = reader.readLine()) != null){
                String pattern = "^#";
                Pattern r = Pattern.compile(pattern);
                Matcher m = r.matcher(tmpString);
                // 在header 中找到key的index
                if (m.find()) {
                    String line = tmpString.replaceAll("^#","");
                    String[] items = line.split("\t");
                    for(int i=0;i<items.length;i++){
                        String item = items[i];
                        headers.add(item);
                        if (item.equals(key)){
                            keyIndex = i;
                        }
                    }
                } else {
                    // 解析 表格中的具体内容
                    String[] items = tmpString.split("\t");

                    keyValue = items[keyIndex];
                    Map<String, String> map = new HashMap<>();
                    for (int i = 0; i < items.length; i++) {
                        String item = headers.get(i);
                        String value = items[i];
                        map.put(item, value);
                    }
                    result.put(keyValue, map);
                }
            }
            reader.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return null;
    }
}
